package org.example.com.controller;

import org.example.com.pojo.DiscoverConfig;

import org.example.com.pojo.Result;
import org.example.com.service.DiscoverConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/discover")
public class DiscoverConfigController {

    @Autowired
    private DiscoverConfigService discoverConfigService;

    //根据配置种类获取对应的发现配置
    @GetMapping("/type/{configType}")
    public Result getDiscoverConfigsByType(@PathVariable String configType) {
        List<DiscoverConfig> configs = discoverConfigService.getDiscoverConfigsByType(configType);
        return Result.success(configs);
    }

    //获取所有活动状态的发现配置
    @GetMapping("/active")
    public Result getActiveDiscoverConfigs() {
        List<DiscoverConfig> configs = discoverConfigService.getActiveDiscoverConfigs();
        return Result.success(configs);
    }

    //通过id,获取对应发现配置
    @GetMapping("/{id}")
    public Result getDiscoverConfigById(@PathVariable Integer id) {
        DiscoverConfig config = discoverConfigService.getDiscoverConfigById(id);
        if (config != null) {
            return Result.success(config);
        }
        return Result.error("配置不存在");
    }

    //创建发现配置方法，暂未使用
    @PostMapping
    public Result createDiscoverConfig(@RequestBody DiscoverConfig discoverConfig) {
        boolean success = discoverConfigService.createDiscoverConfig(discoverConfig);
        if (success) {
            return Result.success("创建成功");
        }
        return Result.error("创建失败");
    }

    //修改发现配置方法，暂未使用
    @PutMapping("/{id}")
    public Result updateDiscoverConfig(@PathVariable Integer id, @RequestBody DiscoverConfig discoverConfig) {
        discoverConfig.setId(id);
        boolean success = discoverConfigService.updateDiscoverConfig(discoverConfig);
        if (success) {
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    //删除发现配置方法，暂未使用
    @DeleteMapping("/{id}")
    public Result deleteDiscoverConfig(@PathVariable Integer id) {
        boolean success = discoverConfigService.deleteDiscoverConfig(id);
        if (success) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    //添加一个响应方法，一次返回所有数据给前端渲染
    @GetMapping("/info")
    public Result getDiscoverInfo() {
        Map<String, Object> discoverInfo = new HashMap<>();

        // 获取弹幕列表1
        List<DiscoverConfig> danMuList1 = discoverConfigService.getDiscoverConfigsByType("danmu_1");
        discoverInfo.put("danMuList1", danMuList1);

        // 获取弹幕列表2
        List<DiscoverConfig> danMuList2 = discoverConfigService.getDiscoverConfigsByType("danmu_2");
        discoverInfo.put("danMuList2", danMuList2);

        // 获取主帖信息
        List<DiscoverConfig> mainPost = discoverConfigService.getDiscoverConfigsByType("main_post");
        discoverInfo.put("postZone", mainPost);

        // 获取内容块列表
        List<DiscoverConfig> contentBlockList = discoverConfigService.getDiscoverConfigsByType("content_block");
        discoverInfo.put("contentBlockList", contentBlockList);

        // 获取福利列表
        List<DiscoverConfig> benefits = discoverConfigService.getDiscoverConfigsByType("benefits");
        discoverInfo.put("benefits", benefits);

        return Result.success(discoverInfo);
    }

}
